﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Data;
using MPMusicVids.DVD;
using SQLite.NET;
using System.Collections;
using System.Drawing.Drawing2D;
using MediaPortal.Configuration;

namespace MPMusicVids
{
    public partial class DVDPanel : UserControl
    {
        DataManager dm = MusicVidsMain.dm;
        Log.Logging log = Log.Logging.getLogger();
        DVDInfo dvd = new DVDInfo();
        SQLiteResultSet rs;

        public DVDPanel()
        {
            InitializeComponent();
            //textBox1.Text = dvd.logDiscAttributes();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //Add a iso or ifo file
            getFile.Filter = "Image or Ifo files|*.b5t;*.b6t;*.bwt;*.ccd;*.cdi;*.cue;*.iso;*.mds;*.nrg;*.pdi;*.isz;*.ifo";
            getFile.FilterIndex = 1;
            getFile.Title = "Select ISO/IFO File";
            if (getFile.ShowDialog() == DialogResult.OK)
            {
                dgvDVD.Rows.Add();
                if (new addDVD().ShowDialog() == DialogResult.OK)
                    reLoad();
            }

        }

        private void DVDPanel_Load(object sender, EventArgs e)
        {
            //reLoad();
        }

        public void reLoad()
        {
            //Reload the main window to show contents of DB
            ArrayList dvds = dm.getAllDVD();
            dvdList.Items.Clear();
            foreach (DVDItem dvd in dvds)
            {               
                dvdList.Items.Add(dvd);               
            }
            try
            {
                dvdList.SelectedIndex = 0;
            }
            catch{}
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadArtist();
        }

        private void LoadArtist()
        {
            //Populate all that with the stuff
            DVDItem selectedDVD = (DVDItem)dvdList.SelectedItem;
            try
            {
                Image aImg = Image.FromFile(selectedDVD.Artist.Image);
                artistImg.Image = aImg;
                SetImage(artistImg);
            }
            catch
            {
                artistImg.Image = artistImg.InitialImage;
            }
            //artistName.Text = selectedDVD.Artist.Name;
            //artistBio.Text = selectedDVD.Artist.Bio;
            foreach (DVDTrack track in selectedDVD.Tracks)
                trackList.Items.Add(track);
        }

        //Generate new image dimensions
        public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {
            double multiplier = 0;
            string layout;
            if (currH > currW) layout = "portrait";
            else layout = "landscape";

            switch (layout.ToLower())
            {
                case "portrait":
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;
                    else
                        multiplier = (double)destH / (double)currH;
                    break;
                case "landscape":
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;
                    else
                        multiplier = (double)destH / (double)currH;
                    break;
            }
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }

        private void SetImage(PictureBox pb)
        {
            try
            {
                Image img = pb.Image;
                Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height);
                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);
                Graphics gfx = Graphics.FromImage(img);
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                pb.Image = null;
                pb.SizeMode = PictureBoxSizeMode.CenterImage;
                pb.Image = finalImg;
            }
            catch (System.Exception e)
            {

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            mainConfig mc = (mainConfig)this.Parent.Parent.Parent;
            mc.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
